//
//  SYDGradientPolylineRenderer.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/15.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//
//GradientPolylineRenderer.m实现：(上面的几个宏定义，是速度的边界值，以及对应的颜色边界值；另外我这里会对hues[i]是否为0做判断，项目需求要区分暂停点和速度过快点，已防作弊，此种情况会用虚线代替，如果只绘制渐变实线，不用管这)

#import "SYDGradientPolylineRenderer.h"
#import <pthread.h>
#import "SYDGradientPolylineOverlay.h"


#define V_MAX 4.5
#define V_MIN 1.0
#define H_MAX 0.33
#define H_MIN 0.03

@implementation SYDGradientPolylineRenderer{
    float* hues;
    pthread_rwlock_t rwLock;
    SYDGradientPolylineOverlay* polyline;
}
//
//- (id) initWithOverlay:(id<MAOverlay>)overlay{
//    self = [super initWithOverlay:overlay];
//    if (self){
//        pthread_rwlock_init(&rwLock,NULL);
//        polyline = ((SYDGradientPolylineOverlay*)self.overlay);
//        float *velocity = polyline.velocity;
//        int count = (int)polyline.pointCount;
//        [self velocity:velocity ToHue:&hues count:count];
//        [self createPath];
//    }
//    return self;
//}
///***/
//- (void) velocity:(float*)velocity ToHue:(float**)_hue count:(int)count{
//    *_hue = malloc(sizeof(float)*count);
//    for (int i=0;i<count;i++){
//        float curVelo = velocity[i];
//        //        //原有渐变公式
//        //        curVelo = ((curVelo < V_MIN) ? V_MIN : (curVelo  > V_MAX) ? V_MAX : curVelo);
//        //        (*_hue)[i] = H_MIN + ((curVelo-V_MIN)*(H_MAX-H_MIN))/(V_MAX-V_MIN);
//        
//        if(curVelo>0.){
//            curVelo = ((curVelo < V_MIN) ? V_MIN : (curVelo  > V_MAX) ? V_MAX : curVelo);
//            (*_hue)[i] = H_MIN + ((curVelo-V_MIN)*(H_MAX-H_MIN))/(V_MAX-V_MIN);
//        }else{
//            //暂停颜色
//            (*_hue)[i] = 0.;
//        }
//    }
//}
//
//-(void) createPath{
//    CGMutablePathRef path = CGPathCreateMutable();
//    BOOL pathIsEmpty = YES;
//    for (int i=0;i< polyline.pointCount;i++){
//        CGPoint point = [self glPointForMapPoint:polyline.points[i]];
//        CGPoint point = [self pointForMapPoint:polyline.points[i]];
//        if (pathIsEmpty){
//            CGPathMoveToPoint(path, nil, point.x, point.y);
//            pathIsEmpty = NO;
//        } else {
//            CGPathAddLineToPoint(path, nil, point.x, point.y);
//        }
//    }
//    
//    pthread_rwlock_wrlock(&rwLock);
//    self.path = path;
//    pthread_rwlock_unlock(&rwLock);
//}
//
////-(BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale{
////    CGRect pointsRect = CGPathGetBoundingBox(self.path);
////    CGRect mapRectCG = [self rectForMapRect:mapRect];
////    return CGRectIntersectsRect(pointsRect, mapRectCG);
////}
//
//- (void) drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
//    
//    //put this blok into the canDraw method cause problem
//    CGRect pointsRect = CGPathGetBoundingBox(<#CGPathRef  _Nullable path#>)
//    CGRect pointsRect = CGPathGetBoundingBox(self.path);
//    CGRect mapRectCG = [self rectForMapRect:mapRect];
//    if (!CGRectIntersectsRect(pointsRect, mapRectCG))return;
//    CGContextSetLineCap(context, kCGLineCapRound);
//    CGContextSetLineJoin(context, kCGLineJoinRound);
//    UIColor* pcolor,*ccolor;
//    for (int i=0;i< polyline.pointCount;i++){
//        CGPoint point = [self pointForMapPoint:polyline.points[i]];
//        CGMutablePathRef path = CGPathCreateMutable();
//        
//        if(hues[i]==0.){
//            //虚线
//            if(i==0){
//                CGPathMoveToPoint(path, nil, point.x, point.y);
//            }else{
//                //颜色
//                CGContextSetRGBStrokeColor(context, 153.0 / 255.0, 153.0 / 255.0, 153.0 / 255.0, 1.0);
//                //线宽
//                CGFloat lineWidth = CGContextConvertSizeToUserSpace(context, (CGSize){self.lineWidth,self.lineWidth}).width;
//                CGContextSetLineWidth(context, lineWidth);
//                
//                CGFloat lengths[] = {lineWidth*2,lineWidth*2};//设置虚线
//                CGContextSetLineDash(context, lineWidth, lengths, 2);//设置虚线
//                
//                CGPoint prevPoint = [self pointForMapPoint:polyline.points[i-1]];
//                CGPathMoveToPoint(path, nil, prevPoint.x, prevPoint.y);
//                CGPathAddLineToPoint(path, nil, point.x, point.y);
//                CGContextAddPath(context, path);
//                CGContextStrokePath(context);
//            }
//        }else{
//            //跑步渐变
//            ccolor = [UIColor colorWithHue:hues[i] saturation:1.0f brightness:1.0f alpha:1.0f];
//            if (i==0){
//                CGPathMoveToPoint(path, nil, point.x, point.y);
//            } else {
//                CGPoint prevPoint = [self pointForMapPoint:polyline.points[i-1]];
//                CGPathMoveToPoint(path, nil, prevPoint.x, prevPoint.y);
//                CGPathAddLineToPoint(path, nil, point.x, point.y);
//                
//                CGFloat pc_r,pc_g,pc_b,pc_a,
//                cc_r,cc_g,cc_b,cc_a;
//                
//                [pcolor getRed:&pc_r green:&pc_g blue:&pc_b alpha:&pc_a];
//                [ccolor getRed:&cc_r green:&cc_g blue:&cc_b alpha:&cc_a];
//                
//                CGFloat gradientColors[8] = {pc_r,pc_g,pc_b,pc_a,
//                    cc_r,cc_g,cc_b,cc_a};
//                
//                CGFloat gradientLocation[2] = {0,1};
//                CGContextSaveGState(context);
//                CGFloat lineWidth = CGContextConvertSizeToUserSpace(context, (CGSize){self.lineWidth,self.lineWidth}).width;
//                CGPathRef pathToFill = CGPathCreateCopyByStrokingPath(path, NULL, lineWidth, self.lineCap, self.lineJoin, self.miterLimit);
//                CGContextAddPath(context, pathToFill);
//                CGContextClip(context);
//                CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//                CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, gradientLocation, 2);
//                CGColorSpaceRelease(colorSpace);
//                CGPoint gradientStart = prevPoint;
//                CGPoint gradientEnd = point;
//                CGContextDrawLinearGradient(context, gradient, gradientStart, gradientEnd, kCGGradientDrawsAfterEndLocation);
//                CGGradientRelease(gradient);
//                CGContextRestoreGState(context);
//                pcolor = [UIColor colorWithCGColor:ccolor.CGColor];
//            }
//        }
//    }
//}
@end
